import 'package:flutter_test/flutter_test.dart';
import 'package:awak/page/community/topic_data_holder.dart';
import 'package:awak/common/models/topic.dart';

void main() {
  group('TopicDataHolder Tests', () {
    setUp(() {
      // 重置状态
      TopicDataHolder.allTopics.clear();
      TopicDataHolder.joinedTopicIds.clear();
      TopicDataHolder.topicPeopleCache.clear();
      TopicDataHolder.currentTopic = null;
      TopicDataHolder.currentTopicId = null;
    });

    test('should join topic and update people count', () {
      // 创建测试主题
      final topic = Topic(
        id: 1,
        title: 'Test Topic',
        coverImage: 'test.jpg',
        content: '10',
        label: 'test',
        numberPeople: 98,
        userId: 1,
      );

      TopicDataHolder.allTopics = [topic];
      TopicDataHolder.currentTopic = topic;
      TopicDataHolder.currentTopicId = 1;

      // 测试加入主题
      expect(TopicDataHolder.isTopicJoined(1), false);
      
      TopicDataHolder.joinTopic(1);
      expect(TopicDataHolder.isTopicJoined(1), true);

      // 测试更新人数
      TopicDataHolder.updateTopicPeopleCount(1, 99);
      expect(TopicDataHolder.getTopicPeopleCount(1), 99);
      
      // 验证缓存中的数据
      expect(TopicDataHolder.topicPeopleCache[1], 99);
      
      // 验证通过getTopicById获取的数据
      final updatedTopic = TopicDataHolder.getTopicById(1);
      expect(updatedTopic?.numberPeople, 99);
    });

    test('should cache people count correctly', () {
      final topic = Topic(
        id: 2,
        title: 'Test Topic 2',
        coverImage: 'test2.jpg',
        content: '5',
        label: 'test2',
        numberPeople: 50,
        userId: 2,
      );

      TopicDataHolder.allTopics = [topic];

      // 更新人数到缓存
      TopicDataHolder.updateTopicPeopleCount(2, 55);
      
      // 验证缓存优先级
      expect(TopicDataHolder.getTopicPeopleCount(2), 55);
      
      // 验证原始数据没有被直接修改（只有通过copyWith）
      final originalTopic = TopicDataHolder.allTopics.first;
      expect(originalTopic.numberPeople, 55); // 应该已经被更新
    });

    test('should handle null topic id gracefully', () {
      expect(TopicDataHolder.isTopicJoined(null), false);
      expect(TopicDataHolder.getTopicPeopleCount(null), 0);
      
      // 这些操作不应该抛出异常
      TopicDataHolder.joinTopic(null);
      TopicDataHolder.updateTopicPeopleCount(null, 100);
    });

    test('should get recommendation topics correctly', () {
      final topics = [
        Topic(id: 1, title: 'Topic 1', coverImage: 'test1.jpg', content: '1', label: 'label1', numberPeople: 10, userId: 1),
        Topic(id: 2, title: 'Topic 2', coverImage: 'test2.jpg', content: '2', label: 'label2', numberPeople: 20, userId: 2),
        Topic(id: 3, title: 'Topic 3', coverImage: 'test3.jpg', content: '3', label: 'label3', numberPeople: 30, userId: 3),
      ];

      TopicDataHolder.allTopics = topics;
      TopicDataHolder.currentTopicId = 1;

      final recommendations = TopicDataHolder.getRecommendationTopics();
      expect(recommendations.length, 2);
      expect(recommendations.any((t) => t.id == 1), false); // 当前主题不应该在推荐中
      expect(recommendations.any((t) => t.id == 2), true);
      expect(recommendations.any((t) => t.id == 3), true);
    });
  });
}
